<?php 
/*
 module:		用户管理
 create_time:	2022-04-07 18:34:03
 author:		
 contact:		
*/

namespace app\backend\service;
use app\backend\model\Member;
use app\backend\model\Member as MemberModel;
use app\backend\model\ZhMatchmaker;
use app\backend\model\ZhTopCard;
use think\exception\ValidateException;
use xhadmin\CommonService;

class MemberService extends CommonService {


	/*
 	* @Description  列表数据
 	*/
	public static function indexList($where,$field,$order,$limit,$page){
		try{
			$res = Member::where($where)->field($field)->order($order)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();
            $pids = [];
            $matchmakerIds = [];
            foreach($res['data'] as $v){
                if($v['matchmaker_id'] > 0){
                    $matchmakerIds[] = $v['matchmaker_id'];
                }
//                if($v['p_id'] > 0){
//                    $pids[] = $v['p_id'];
//                }
            }
            //print_r($matchmakerIds);exit;
            $matchmakerList = [];
//            if(! empty($pids)){
//                $matchmakerList = ZhMatchmaker::whereIn('u_id', $pids)->column('name', 'u_id');
//            }
            if(! empty($matchmakerIds)){
                $matchmakerList = ZhMatchmaker::whereIn('matchmaker_id', $matchmakerIds)->column('name', 'matchmaker_id');
            }
			for($i=0;$i<count($res['data']);$i++){
                /*
                $matchmaker_id=intval($res['data'][$i]["matchmaker_id"]);
                if($matchmaker_id>0){
                    $matchmaker=MemberModel::find($matchmaker_id);
                    if($matchmaker){
                        $res['data'][$i]["matchmaker_id"]=$matchmaker->real_name;
                    }
                }else{
                    $res['data'][$i]["matchmaker_id"]="暂无";
                }*/
                $tmpPid = $res['data'][$i]['matchmaker_id'];
                if($tmpPid > 0){
                    if(isset($matchmakerList[$tmpPid])){
                        $res['data'][$i]["matchmaker_id"] = $matchmakerList[$tmpPid];
                    } else{
                        $res['data'][$i]["matchmaker_id"]='-';//"暂无";
                    }
                }else{
                    $res['data'][$i]["matchmaker_id"]='-';//"暂无";
                }

                $top_card_id=intval($res['data'][$i]["top_card_id"]);
                if($top_card_id>0){
                    $card= ZhTopCard::find($top_card_id);
                    $res['data'][$i]["top_card_name"]=$card->name;
                }

            }
		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}
		return ['rows'=>$res['data'],'total'=>$res['total']];
	}

    /*
     * @Description  添加
     * @author lwx
     */
    public static function add($data){
        try{
            validate(\app\backend\validate\Member::class)->scene('add')->check($data);
            $data['wxapp_id'] = session('backend.wxapp_id');
            $data['auth_status']=1;//默认待审核
            $data['create_time'] = time();
            $res = Member::create($data);
        }catch(ValidateException $e){
            throw new ValidateException ($e->getError());
        }catch(\Exception $e){
            abort(config('my.error_log_code'),$e->getMessage());
        }
        if(!$res){
            throw new ValidateException ('操作失败');
        }
        return $res->id;
    }
    
    /*
     * @Description  修改
     */
    public static function update($data){
        try{
            $res = Member::update($data);
        }catch(ValidateException $e){
            throw new ValidateException ($e->getError());
        }catch(\Exception $e){
            abort(config('my.error_log_code'),$e->getMessage());
        }
        if(!$res){
            throw new ValidateException ('操作失败');
        }
        return $res;
    }




}

